﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Web.UI;
using WinnerCMS.BLL;
using WinnerCMS.BLL.PageFactory;
using WinnerCMS.BLL.PageFactory.TagModule;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.WebFactory
{
    public class SpecialPage : PublicPage
    {
        private byte _ModelId;

        private int _SpecialID;
        protected int PageNo = 1;

        #region 重写事件
        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion



        private void Page_Load(object sender, System.EventArgs e)
        {
            if (byte.TryParse(Request.QueryString["ModelId"], out this._ModelId) == false)
            {
                Response.Write("参数错误"); return;
            }

            this._SpecialID = int.Parse(Request.QueryString["SpecialID"]);

            if (Request.QueryString["PageNo"] != null)
            {
                this.PageNo = int.Parse(Request.QueryString["PageNo"]);
            }

        }

        #region 重写Render
        protected override void Render(HtmlTextWriter writer)
        {
            StringBuilder winner = new StringBuilder();
            using (TextWriter tempWriter = new StringWriter())
            {
                base.Render(new HtmlTextWriter(tempWriter));
                winner.Append(tempWriter.ToString());
            }
             
            IList<string> matchresult;

            #region 通用模型标签
            matchresult = TagEngine.TagCollection(winner.ToString(), "Slide");
            foreach (string nextmatch in matchresult)
            {
                winner.Replace(nextmatch, PageCommon.Slide(nextmatch));
            }
              
             
            matchresult = TagEngine.TagCollection(winner.ToString(), "PageList");
            foreach (string nextmatch in matchresult)
            {
                winner.Replace(nextmatch, PageCommon.SpecialContetnList(winner, this._ModelId, this._SpecialID, this.PageNo));
            }
            #endregion

             
             

            SpecialCommon.SpecialInfo(winner, this._SpecialID);

            if (Request["Method"] != null)
            {
                Special model = new SpecialManage().GetInfo(this._SpecialID);
                string Path = StaticFilePath.SpecialUrl(model, true, this.PageNo);
                new FileIO().WriterFile(HttpServer.MapPath(Path), winner.ToString());
            }
            else
            {
                writer.Write(winner.ToString());
            }
        }
        #endregion
    }
}
